using System;
using System.Collections.Generic;
using System.Text;
using WindowsLive.Writer.Api;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;

namespace LWSnippingToolPlugin
{

    [WriterPluginAttribute("fa16d486-8624-4a0d-baf0-d56688f1948c", "Snipping Tool Intergration", ImagePath = "icon.png", PublisherUrl = "http://sharpsoft.net", Description = "Integrates Microsoft Snipping Tool into Windows Live Writer application", Id = "fa16d486-8624-4a0d-baf0-d56688f1948c", HasEditableOptions = false)]
    [InsertableContentSource("Snipping Tool Intergration", MenuText = "snip capture", SidebarText = "snip capture")]
    public class Plugin : ContentSource
    {
        Process m_snipProcess = null;
        internal string m_pathVista = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\SnippingTool.exe";
        internal string m_pathXp = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + @"\Microsoft Experience Pack\Snipping Tool\SnippingTool.exe";
        string m_path = string.Empty;
        Image m_clipImage = null;

        public override void Initialize(IProperties pluginOptions)
        {
            base.Initialize(pluginOptions);

            if (File.Exists(m_pathVista))
            {
                m_path = m_pathVista;
            }
            else if (File.Exists(m_pathXp))
            {
                m_path = m_pathXp;
            }
            else
            {
                throw new NotSupportedException(Resource.msgNotInstalled);
            }
        }
        public override DialogResult CreateContent(IWin32Window dialogOwner, ref string content)
        {
            if (m_path != string.Empty)
            {
                Clipboard.Clear();
                m_snipProcess = Process.Start(m_path);
                ((Form)dialogOwner).WindowState = FormWindowState.Minimized;
            }
            else
            {
                throw new NotSupportedException(Resource.msgNotInstalled);
            }

            if (m_snipProcess != null)
            {
                m_snipProcess.WaitForExit();
                if (Clipboard.ContainsImage())
                {
                    m_clipImage = Clipboard.GetImage();
                    ((Form)dialogOwner).WindowState = FormWindowState.Normal;
                    ((Form)dialogOwner).Activate();
                    ((Form)dialogOwner).Focus();
                }

                if (m_clipImage != null)
                {

                    //insert image
                    //ApplicationManager.CommandManager.Get(CommandId.Paste).PerformExecute();
                    //SendMessage(new HandleRef(dialogOwner, dialogOwner.Handle), WM_PASTE, IntPtr.Zero, IntPtr.Zero);
                    SendKeys.Send("^v");                  

                    return DialogResult.OK;
                }
                else
                {
                    if (MessageBox.Show(Resource.msgNoImage, "Empty clipboard", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        return CreateContent(dialogOwner, ref content);
                    }
                }
            }
            return DialogResult.Cancel;
        }

        //private const uint WM_PASTE = 0x0302;
        //[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        //static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);


    }
}
